<?php

namespace app\admin\service;

use think\facade\{View, Session, Db};
use app\BaseController;
use think\exception\HttpResponseException;

class Priceup extends BaseController
{
    //录入金额
    public function priceAdd($uid,$price,$ext_id,$remark,$type=1,$price_type=1){
        // 启动事务
        Db::startTrans();
        try {
            $sn=time().$uid;
            Db::name('iuser_balance')->insert([
                'sn'=>$sn,
                'price_type'=> $price_type,
                'type' => $type,
                'price'=>$price,
                'iuser_id'=>$uid,
                'ext_id'=>$ext_id,
                'addtime'=>time(),
                'remark'=>$remark

            ]);

            Db::name('iuser')->where("id",$uid)->inc('balance', $price)->update();
            Db::name('iuser')->where("id", $uid)->inc('balance_sum', $price)->update();
            
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
           
           return ['code' => 0,'message'=>$e->getMessage()];
        }
        return ['code'=>1];
    } 
    //扣减金额
    public function priceDec($uid, $price, $ext_id, $remark, $type = 0, $price_type = 0){
        // 启动事务
        Db::startTrans();
        try {
            $sn = time() . $uid;
            Db::name('iuser_balance')->insert([
                'sn' => $sn,
                'price_type' => $price_type,
                'type' => $type,
                'price' => $price,
                'iuser_id' => $uid,
                'ext_id' => $ext_id,
                'addtime' => time(),
                'remark' => $remark

            ]);

            Db::name('iuser')->where("id", $uid)->dec('balance', $price)->update();
          //  Db::name('iuser')->where("id", $uid)->dec('balance_sum', $price)->update();

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code' => 0, 'message' => $e->getMessage()];
        }
        return ['code' => 1];
    }


}
